const app = getApp()
const $api = require('../../utils/api.js').API;

Page({
    data: {
        list:{},
        imgUrl:app.globalData.imgUrl,
        cate:{},
        cateId:1,
        index:0,
        banner:'',
        key:0,
        page:1,
        pageShow:true
    },
    onLoad: function(){
        wx.showLoading()
        this.getCate();
        this.getBanner()
    },
    getBanner:function(){
        var that = this;
        var data = {'type':2}
        $api.get('banner',data).then(res => {
            //请求成功
            if(res.data.code == 1){
              that.setData({
                banner:res.data.data[0].img
              })
            }
        })
      },
    getCate:function(){
        var that = this
        var data = {'role':wx.getStorageSync('role')}
        $api.get('score/cate',data).then(res =>{
            //请求成功
            if(res.data.code == 1){
                var cate = res.data.data
                that.setData({
                    cate:cate,
                    cateId:cate[0]['id']
                })
                that.getlist();
            }
        })
    },
    getlist:function(){
        var data = {'cateId':this.data.cateId,page:this.data.page}
        var that = this
        var list = this.data.list
        $api.get('score/goods',data).then(res => {
            if(res.data.data.total == 0){
                that.setData({
                    noneShow:true
                })
            }else{
                that.setData({
                    noneShow:false
                })
            }
            //请求成功
            if(res.data.code == 1){
                if(res.data.data.last_page <= that.data.page){
                    that.setData({
                        pageShow:false
                    })
                }
                wx.hideLoading()
                if(that.data.page == 1){
                    that.setData({
                        list:res.data.data.data
                    })
                }else{
                    that.setData({
                        list:list.concat(res.data.data.data)
                    })
                }
            }
        }).catch(err => {
            //请求失败
            //console.log('job','请求失败');
        })
    },
    select:function(e){
        var id = e.target.dataset.id;
        var key = e.target.dataset.key;
        this.setData({
          cateId:id,
          key:key,
          page:1
        })
        wx.showLoading()
        this.getlist()
    },
    info:function(e){
        var id = e.currentTarget.dataset.id
        wx.navigateTo({
            url:'/pages/pointsMall/info?id='+id
        })
    },
    onReachBottom: function(){
        if(this.data.pageShow){
            //当前不是最后一页可加载
            this.setData({
                page: this.data.page + 1,
            })
            wx.showLoading()
            this.getlist();
        }
    },
    onShareAppMessage: function() {
        return {
            title: "积分商城-天意美业直通车",
            path: "/pages/index/index?puid="+wx.getStorageSync('uid')
        };
    }
});